package com.qiwenshare.file.util;





import com.qiwenshare.file.exception.QiwenFileException;

import java.util.Collection;
import java.util.function.Supplier;

/**
 * 断言工具，不满足则抛出异常
 */
public class AssertUtils {

    /**
     * 断言不为null，否则抛出异常
     * @param o
     * @param e
     * @throws Exception
     */
    public static void notNull(Object o , QiwenFileException e) {
        if (o==null){
            throw e;
        }
    }
    public static void notNull(Object o , Supplier<? extends QiwenFileException> e) {
        if (o==null){
            throw e.get();
        }
    }

    public static void notEmpty(Object o , QiwenFileException e) {
        if (o==null){
            throw e;
        }
        if (o instanceof  String){
            String s = (String) o;
            if ("".equals(s)|| "".equals(s.trim())){
                throw e;
            }
        }else if (o instanceof Collection){
            Collection c =(Collection) o;
            if (c.size()==0){
                throw e;
            }
        }else {
            throw e;
        }
    }

    /**
     * 断言null，否则抛出异常
     * @param o
     * @param e
     * @throws Exception
     */
    public static void isNull(Object o ,QiwenFileException e) {
        if (o!=null){
            throw e;
        }
    }

    /**
     * 断言exp bool表达式为真，否则抛出异常
     * @param exp
     * @param e
     */
    public static void exp(boolean exp ,QiwenFileException e) {
        if (!exp){
            throw e;
        }
    }
    public static void exp(boolean exp , Supplier<? extends QiwenFileException> e) {
        if (!exp){
            throw e.get();
        }
    }
    /**
     *
     * @param exp bool表达式为真
     * @param expect 期望 真或者假 与期望值不一致时抛出异常
     * @param e
     */
    public static void exp(boolean exp ,boolean expect,QiwenFileException e) {
        if (exp != expect){
            throw e;
        }
    }
    public static void exp(boolean exp,boolean expect , Supplier<? extends QiwenFileException> e) {
        if (exp != expect){
            throw e.get();
        }
    }
}

